package cn.ydt.ydt.shopping.webapi.controller;

import cn.ydt.ydt.common.resful.JsonResult;
import cn.ydt.ydt.pojo.shopping.dto.AddVegetableDTO;
import cn.ydt.ydt.pojo.shopping.dto.CreateVegetableOrderDTO;
import cn.ydt.ydt.pojo.shopping.dto.SimpleVegetableDTO;
import cn.ydt.ydt.pojo.shopping.model.Vegetable;
import cn.ydt.ydt.pojo.shopping.model.VegetableElastic;
import cn.ydt.ydt.pojo.shopping.vo.DetailOrderCartVO;
import cn.ydt.ydt.pojo.shopping.vo.OrderCartVO;
import cn.ydt.ydt.pojo.shopping.vo.SimpleCartVO;
import cn.ydt.ydt.pojo.shopping.vo.SimpleVegetableVO;
import cn.ydt.ydt.shopping.service.ISearchService;
import cn.ydt.ydt.shopping.service.IVegetableService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vegetable")
@Api(tags = "菜品管理模块")
public class VegetableController {
    @Autowired
    IVegetableService vegetableService;

    @Autowired
    ISearchService searchService;


    @PostMapping("/add/vegetable")
    @ApiOperation("添加菜品")
    public JsonResult<Void> addVegetable(@Validated @RequestBody AddVegetableDTO addVegetableDTO){
        vegetableService.addVegetable(addVegetableDTO);
        return JsonResult.ok("添加成功！");
    }

    @GetMapping("/sort/{vegetableSort}")
    @ApiOperation("查询对应类别的菜品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "类别名",name = "vegetableSort",dataType = "String",example = "蔬菜")
    })
    public JsonResult<List<SimpleVegetableVO>> getVegetableListBySort(@PathVariable String vegetableSort){
        List<SimpleVegetableVO> list = vegetableService.getVegetableService(vegetableSort);
        return JsonResult.ok(list);
    }

    @PostMapping("/cart/add")
    @ApiOperation("添加购物车")
    public JsonResult<Void> addVegetableCart(@Validated @RequestBody SimpleVegetableDTO simpleVegetableDTO){
        vegetableService.addVegetableCart(simpleVegetableDTO);
        return JsonResult.ok();
    }

    @GetMapping("/del/{cartId}")
    @ApiOperation("删除购物车方法")
    public JsonResult<Void> delVegetable(@PathVariable Long cartId){
        vegetableService.delVegetableCart(cartId);
        return JsonResult.ok("删除购物车成功");
    }

    @PostMapping("/add/order")
    @ApiOperation("新增订单方法")
    public JsonResult<Void> addVegetable(@Validated  @RequestBody CreateVegetableOrderDTO orderDTO){
        vegetableService.addVegetableOrder(orderDTO);
        return JsonResult.ok("订单生成成功");
    }

    @GetMapping("/update/{orderId}")
    @ApiOperation("订单完成接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单id",name = "orderId",required = true,dataType = "Long",example = "5")
    })
    public JsonResult<Void> updateOrderComplete(@PathVariable Long orderId){
        vegetableService.updateOrderComplete(orderId);
        return JsonResult.ok();
    }

    @GetMapping("/order/sort")
    @ApiOperation("查询当前登录用户对应的订单信息")
    public JsonResult<List<OrderCartVO>> getOrderListByUser(){
        List<OrderCartVO> list = vegetableService.getOrderCartByOrderId();
        return JsonResult.ok(list);
    }


    @GetMapping("/search/{key}")
    @ApiOperation("查询菜品信息")
    public JsonResult<List<VegetableElastic>> getVegetableList(@PathVariable String key){
        searchService.clearVegetable();
        searchService.loadVegetable();
        List<VegetableElastic> list = searchService.search(key);
        return JsonResult.ok(list);
    }

    @GetMapping("/cart")
    @ApiOperation("查询登录用户的所有购物车信息")
    public JsonResult<List<SimpleCartVO>> getCartListByUserId(){
        List<SimpleCartVO> cartList = vegetableService.getCartByUserId();
        return JsonResult.ok(cartList);
    }

    @GetMapping("/get/{vegetableId}")
    @ApiOperation("根据菜品id查询菜品信息")
    public JsonResult<Vegetable> getVegetableById(@PathVariable Long vegetableId){
        Vegetable vegetable = vegetableService.getVegetableById(vegetableId);
        return JsonResult.ok(vegetable);
    }

    @GetMapping("/get/byName/{name}")
    @ApiOperation("根据菜品名查询菜品信息")
    public JsonResult<Vegetable> getVegetableByName(@PathVariable String name){
        Vegetable vegetable  = vegetableService.getVegetableByName(name);
        return JsonResult.ok(vegetable);
    }

    @GetMapping("/get/cart/{cartId}")
    @ApiOperation("根据购物车id查询购物车信息")
    public JsonResult<SimpleCartVO> getCartById(@PathVariable Long cartId){
        SimpleCartVO cartVO = vegetableService.getCartById(cartId);
        return JsonResult.ok(cartVO);
    }

    @GetMapping("/get/detail/order")
    @ApiOperation("查询所有订单详细信息")
    public JsonResult<List<DetailOrderCartVO>> getDetailOrder(){
        List<DetailOrderCartVO> detailOrderCartVOS = vegetableService.getOrderCart();
        return JsonResult.ok(detailOrderCartVOS);
    }

    @GetMapping("/get/detailOrder/user")
    @ApiOperation("查询当前登录用户所有订单详细信息")
    public JsonResult<List<DetailOrderCartVO>> getDetailOrderByUserId(){
        List<DetailOrderCartVO> detailOrderCartVOS = vegetableService.getDetailOrderByUserId();
        return JsonResult.ok(detailOrderCartVOS);
    }
}
